package cn.ilovese.collect.serviceimpl.work;

import cn.ilovese.collect.datamapper.task.TaskMapper;
import cn.ilovese.collect.datamapper.work.WorkMapper;
import cn.ilovese.collect.po.work.Work;
import cn.ilovese.collect.service.work.WorkService;
import cn.ilovese.collect.util.Constant;
import cn.ilovese.collect.vo.ResultVO;
import cn.ilovese.collect.vo.work.WorkVO;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@CacheConfig(cacheNames = "work",cacheManager = "cacheManager")
public class WorkServiceImpl implements WorkService {

    @Resource
    WorkMapper workMapper;
    @Resource
    TaskMapper taskMapper;

    @Override
    public ResultVO<WorkVO> pickTask(Integer workid, Integer taskid) {
        Work work = new Work();
        work.setTaskId(taskid);
        work.setUserId(workid);
        work.setStatus(1);
        int id = workMapper.insert(work);
        if(id < 1)
            return new ResultVO<>(Constant.REQUEST_FAIL, "插入失败！");
        else{
            //该任务的need_num需要-1
            taskMapper.decreaseNeedNum(taskid);
            WorkVO workVO = new WorkVO(workMapper.selectByWorkerAndTask(taskid,workid));
            return new ResultVO<>(Constant.REQUEST_SUCCESS, "插入成功",workVO);
        }
    }

    @Override
    public ResultVO<Integer> getTaskStatus(Integer workerid, Integer taskid) {
        Work work = workMapper.selectByWorkerAndTask(taskid,workerid);
        if(work == null)
            return new ResultVO<>(Constant.REQUEST_FAIL, "没有领取该任务");
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"该任务已领取",work.getStatus());
    }
    // @Resource标识特定的目标组件，你们自己添加，类似软工二
    // @Autowired装载java bean，用于set方法，你们自己添加，类似软工二

    //注意在这里添加每个实现方法的说明注释，类似以下
    /**
     * 这个方法干了什么
     *
     * @param 参数1 解释
     * @param 参数2 解释
     * ......
     * @return 返回值解释
     */


}
